package org.dei.perla.sys.console;

import java.net.ServerSocket;
import java.net.Socket;

import org.dei.perla.component.Startable;

/**
 * Classe per implementare la console
 * @author Diego
 */
public class ConsoleServer implements Startable {
	
	//TODO Spostarlo nella config di PerLa
	final int PORT = 14567;
	
	private boolean isStarted = false;
	
	public boolean isStarted() {
		return this.isStarted;
	}

	public void start() {
		isStarted = true;
		try 
		{
			System.out.println("SERVER: creating socket\n");
			ServerSocket tServerSocket = new ServerSocket(PORT);
			System.out.println("ciclo\n");
			while (isStarted) {
				System.out.println("SERVER: accepting.... \n");
				Socket tClientSocket = tServerSocket.accept();
				System.out.println("SERVER: accepted, creo thread \n");
				new ServerThread(tClientSocket).run();
			}
		
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void stop() {
		this.isStarted = false;
	} 
	
	
}
